package com.ds.service;

import com.ds.orm.mapper.BlogDao;
import com.ds.orm.mapper.BlogDiscussDao;
import com.ds.orm.model.Blog;
import com.baomidou.mybatisplus.plugins.Page;
import com.ds.bo.BlogGroupBO;
import com.ds.core.base.BaseService;
import com.ds.core.util.MarkUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 博客，记录用户发表的博客，文章，日志 服务实现类
 * </p>
 *
 * @author Du
 * @since 2017-11-28
 */
@Service
public class BlogService extends BaseService<Blog>{
	@Autowired
	private BlogDao blogDao;
	@Autowired
	private BlogDiscussDao blogDiscussDao;
	
	public List<BlogGroupBO> getMonthGroup(long authorId) {
		List<BlogGroupBO> monthGroup=blogDao.getMonthGroup(authorId);
		return monthGroup;
	}
	@Override
	public Blog queryById(Long id) {
		Blog blog=super.queryById(id);
		if (blog.getTags()!=null&&!"".equals(blog.getTags())) {
			String tags=blog.getTags();
			blog.setTagsArr(tags.split(","));
		}
		return blog;
	}
	
	public List<Blog> queryPageList(Map<String, Object> params) {
		List<Blog> blogs=query(params).getRecords();
		List<Blog> blogs2=new ArrayList<>();
		for (Blog blog : blogs) {
			blog.setBlogBody(MarkUtil.htmlToText(blog.getBlogBody()).substring(0, 200));
			if (blog.getTags()!=null&&!"".equals(blog.getTags())) {
				String tags=blog.getTags();
				blog.setTagsArr(tags.split(","));
			}
			blog.setRemark(blogDiscussDao.queryBlogDiscussNum(blog.getId())+"");
			blogs2.add(blog);
		}
		return blogs2;
	}
}
